package StreanmLearn.Middle;

import java.util.ArrayList;
import java.util.Collections;

public class stream流的中间方法1 {
    public static void main(String[] args) {
        /*
    filtre  过滤
    limit   获取前几个元素
    skip  跳过前几个元素

    注意1:中间方法，返回新的Stream流，原来的Stream流只能使用一次，建议所有链式编程
    注意2:修改Stream中的数据，不会影响原来集合或者数组中的数据
     */

        ArrayList<String> list=new ArrayList<>();
        Collections.addAll(list,"张无忌","李四","张强","张三丰","王五","克莱普","谢广坤");

        //filtre  过滤
        filterTest(list);

        //limit   获取前几个元素
        list.stream().limit(3)
                .forEach(s-> System.out.println(s));

        System.out.println("——————————————————————————");
        //skip  跳过前几个元素
        list.stream()
                .skip(3)
                .forEach(s-> System.out.println(s));

        System.out.println("----------------");
        //Collections.addAll(list,"张无忌","李四","张强","张三丰","王五","克莱普");
        //练习  获取 "张强","张三丰","王五"
        list.stream()
                .skip(2)
                .limit(3)
                .forEach(s-> System.out.println(s));

    }
    //filtre  过滤  把张留下 其余数据全部过滤
    public static void filterTest(ArrayList<String> list) {

//        list.stream().filter(new Predicate<String>() {
//            @Override
//            public boolean test(String s) {
//                return s.startsWith("张");
//            }
//        }).forEach(s-> System.out.println(s));

        list.stream()
                .filter(s-> s.startsWith("张"))
                .filter(s->s.length()==3)
                .forEach(s->System.out.println(s));

        System.out.println(list);
    }

}
